<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>金额转换</title>
    <script type="text/javascript">
        /* 中文0~9数字 */
        let caps = "零壹贰叁肆伍陆柒捌玖".split("");
        /* 中文进制 */
        let units = "仟佰拾萬仟佰拾億仟佰拾萬仟佰拾元角分".split("");

        function conversion() {
            let money_number = document.getElementById("money_number").value;
            /* 正则表达式规则——首位为1~9，整数部分最大15位，小数点后2位(可选) */
            let reg = /^(([1-9]\d{0,14})|0)(\.\d{1,2})?$/;

            /* 正则表达式判断 */
            if (!reg.test(money_number)) {
                alert("输入金额格式错误！");
            }

            /* 获取小数点索引 */
            let index = money_number.indexOf(".");
            if (index === -1) { /* 如果不存在*/
                money_number = money_number + ".00";
            } else {
                money_number = money_number + "0";
            }

            /* 重新定位 */
            index = money_number.indexOf(".");

            /* 整数部分 */
            let part_1 = money_number.substring(0, index);
            /* 小数部分 */
            let part_2 = money_number.substring(index + 1, index + 3);

            /* 获取金额数组 */
            let money = (part_1 + part_2).split("");

            /* 大写金额转换 */
            /* 大写金额 */
            let total_amount = "";
            let i = money.length - 1;
            let j = units.length - 1;
            for (; i >= 0; i--, j--) {
                total_amount = caps[money[i]] + units[j] + total_amount;
            }

            /* 银行金额转换 */
            let bank_money_amount = "";
            let k = part_1.length - 1;
            let flag = 0;
            for (; k >= 0; k--) {
                if (flag === 3) {
                    bank_money_amount = "," + bank_money_amount;
                    flag = 0;
                }
                bank_money_amount = part_1[k] + bank_money_amount;
                flag++;
            }
            bank_money_amount += "." + part_2.toString();

            document.getElementById("amount_in_words").innerHTML = total_amount;
            document.getElementById("bank_amount").innerText = bank_money_amount;
        }
    </script>
</head>
<body>
<div>
    <label>
        金额：
        <input type="text" id="money_number">
        <button type="button" onclick="conversion()">转换</button>
        <br>
    </label>
    银行金额：
    <span id="bank_amount"></span><br>
    大写金额：
    <span id="amount_in_words"></span>
</div>
</body>
</html>